package cn.imu.appdevexperiment2020._3activity.chat;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.imu.appdevexperiment2020.R;
import cn.imu.appdevexperiment2020._3activity.chat.bean.Msg;
import cn.imu.appdevexperiment2020.common.BaseActivity;

/**
 * 描述:
 *
 * @author chenconghua
 * @version 1.0
 * @create 2022-11-13 19:10
 */
 public class ChatMainActivity extends BaseActivity {
   private List<Msg> msgList = new ArrayList<>();
   private EditText inputText;
   private Button send;
   private RecyclerView msgRecyclerView;
   private MsgAdapter adapter;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_chat_main);
      initMsgs();
      inputText = (EditText) findViewById(R.id.input_text);
      send = (Button) findViewById(R.id.send);
      msgRecyclerView = (RecyclerView) findViewById(R.id.msg_recycler_view);
      LinearLayoutManager layoutManager = new LinearLayoutManager(this);
      msgRecyclerView.setLayoutManager(layoutManager);
      adapter = new MsgAdapter(msgList);
      msgRecyclerView.setAdapter(adapter);
      send.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            String content = inputText.getText().toString();
            if (!"".equals(content)) {
               Msg msg = new Msg(content, Msg.TYPE_SENT);
               msgList.add(msg);
               adapter.notifyItemInserted(msgList.size() - 1);
               msgRecyclerView.scrollToPosition(msgList.size() - 1);
               inputText.setText("");
            }
         }
      });
   }

   private void initMsgs() {
      Msg msg1 = new Msg("aaaaaaaa.", Msg.TYPE_RECEIVED);
      msgList.add(msg1);
      Msg msg2 = new Msg("bbbbbbbbbb", Msg.TYPE_SENT);
      msgList.add(msg2);
      Msg msg3 = new Msg("cccccccc. ", Msg.TYPE_RECEIVED);
      msgList.add(msg3);
   }

}
